<template>
    <div class="app-container">
        <section>
            <div class="contentBox">
                <div class="title">
                    <span>{{$route.query.title}}</span>
                </div>
                <div class="content" v-loading="loading">
                    <el-form ref="form" :model="form" disabled :rules="rules" class="form"  label-width="135px">
                        
                        
                        <div style="width:100%;display: flex;margin-bottom:20px">
                            <el-form-item label="头像" prop="avatar" ref="avatar" >
                                <vs-uploading
                                width="120px"
                                @verify="verify('avatar',$event)"
                                v-model="form.avatar"
                                ></vs-uploading>
                                <!-- <el-input v-model="form.avatar" placeholder=""></el-input> -->
                            </el-form-item>
                            <el-form-item label="附件" prop="annex" ref="annex" >
                                <vs-uploading
                                width="120px" 
                                :maxNum="5"
                                @verify="verify('annex',$event)"
                                v-model="form.annex"
                                ></vs-uploading>
                            </el-form-item>
                        </div>
                        <el-form-item label="姓名：" prop="name" >
                            <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
                        </el-form-item>
                        <el-form-item label="性别" prop="gender">
                            <el-radio-group v-model="form.gender" style="width:250px">
                                <el-radio :label="1">男</el-radio>
                                <el-radio :label="2">女</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="工号：" prop="number" >
                            <el-input v-model="form.number" placeholder="请输入工号"></el-input>
                        </el-form-item>
                        <el-form-item label="员工生日：" prop="birthday" >
                            <el-date-picker type="date" 
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期" :picker-options="pickerOptions" v-model="form.birthday" style="width: 250px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="手机号：" prop="phone" >
                            <el-input v-model="form.phone" placeholder="请输入手机号" :maxLength='11' ></el-input>
                        </el-form-item>
                        <el-form-item label="职位：" prop="position" >
                            <!-- <el-input v-model="form.position" placeholder="请输入职位"></el-input> -->
                            <el-select v-model="form.position" @change="chaposition" placeholder="请选择职位">
                                <el-option
                                    v-for="item in postList"
                                    :key="item.id"
                                    :label="item.title"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="岗位等级：" prop="grade2" >
                            <el-input v-model="form.grade" readonly placeholder="" ></el-input>
                        </el-form-item>
                        <el-form-item v-if="form.id" label="来源门店：" prop="store_id2" >
                            <el-select v-model="form.source_id" placeholder="请选择来源门店">
                                <el-option
                                    v-for="item in shopList"
                                    :key="item.id"
                                    :label="item.title"
                                    :value="item.id">
                                </el-option>
                                    <!-- :disabled="item.id==form.store_id" -->
                            </el-select>
                        </el-form-item>
                        <el-form-item label="门店：" prop="store_id" >
                            <el-select v-model="form.store_id" placeholder="请选择门店">
                                <el-option
                                    v-for="item in shopList"
                                    :key="item.id"
                                    :label="item.title"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="国籍：" prop="nationality" >
                            <el-select v-model="form.nationality" placeholder="请选择国籍">
                                <el-option label="中国" :value="1"> </el-option>
                                <el-option label="泰国" :value="2"> </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="密码：" prop="password2" >
                            <el-input v-model="form.password" placeholder="密码不填就是原密码" :maxLength='20' ></el-input>
                        </el-form-item>
                        <el-form-item label="入职日期：" prop="into_time" >
                            <el-date-picker
                                v-model="form.into_time"
                                value-format="timestamp"
                                type="date"
                                placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="转正日期：" prop="formal_time">
                            <el-date-picker
                                value-format="timestamp" 
                                v-model="form.formal_time"
                                type="date"
                                placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="技能：" prop="kill" >
                            <el-input v-model="form.kill" placeholder="请输入技能" ></el-input>
                        </el-form-item>
                        <el-form-item label="证件类型：" prop="id_type" >
                            <el-select v-model="form.id_type" placeholder="请选择证件类型">
                                <el-option label="居民身份证" value="居民身份证"> </el-option>
                                <el-option label="护照" value="护照"> </el-option>
                                <el-option label="台港澳通行证" value="台港澳通行证"> </el-option>
                                <el-option label="其它" value="其它"> </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="证件号码：" prop="id_card" >
                            <el-input v-model="form.id_card" placeholder="请输入证件号码" ></el-input>
                        </el-form-item>
                        <el-form-item label="合同到期时间：" prop="expire_time" >
                            <el-date-picker
                                v-model="form.expire_time"
                                value-format="timestamp"
                                type="date"
                                placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="婚姻状况：" prop="marriage" >
                            <el-select v-model="form.marriage" placeholder="请选择婚姻状况">
                                <el-option label="已婚" value="已婚"> </el-option>
                                <el-option label="未婚" value="未婚"> </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="民族：" prop="nation" >
                            <el-input v-model="form.nation" placeholder="请输入民族" ></el-input>
                        </el-form-item>
                        <el-form-item label="户籍地址：" prop="address" >
                            <el-input v-model="form.address" placeholder="请输入户籍地址" ></el-input>
                        </el-form-item>
                        <el-form-item label="学历：" prop="education" >
                            <el-select v-model="form.education" placeholder="请选择学历">
                                <el-option 
                                v-for="(item,i) in ['初中','中专','高中','大专','本科','研究生','博士生','其他']"
                                :key="i"
                                :label="item" 
                                :value="item"
                                > </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="银行卡号：" prop="card_number" >
                            <el-input v-model="form.card_number" placeholder="请输入银行卡号" ></el-input>
                        </el-form-item>
                        <el-form-item label="紧急联系人：" prop="urgent_contacts" >
                            <el-input v-model="form.urgent_contacts" placeholder="请输入紧急联系人" ></el-input>
                        </el-form-item>
                        <el-form-item label="紧急联系人电话：" prop="urgent_phone" >
                            <el-input v-model="form.urgent_phone" placeholder="请输入紧急联系人电话" ></el-input>
                        </el-form-item>
                        <el-form-item v-if="post.type==2" label="前台业绩目标：" prop="reception_performance" >
                            <el-input v-model="form.reception_performance" @input.native="moneyIN" placeholder="请输入前台业绩目标" ></el-input>
                        </el-form-item>


                        <el-form-item label="备注：" prop="note" style="width:100%">
                            <el-input v-model="form.note" placeholder="请输入备注" type="textarea" :rows="4" ></el-input>
                        </el-form-item>
                    </el-form>
                    <!-- <div style="margin-top:40px">
                        <el-button type="primary" :loading="isBtn" style="width:143px" @click="save">确定</el-button>
                        <el-button style="width:143px" @click="$router.go(-1)">取消</el-button>
                    </div> -->
                </div>
            </div>
        </section>
    </div>
</template>

<script>
import VsUploading from '@/components/VsUploading.vue';
export default {
    components:{
        VsUploading
    },
    data() {
       var url = (rule, value, callback) => {
            // 图片验证
            if (!this.form.avatar) { //hasFmt设置为true/false
                callback(new Error("请上传头像!"));
            } else {
                callback();
            }
        }
        var url2 = (rule, value, callback) => {
            // 图片验证
            if (this.form.annex.length<3) { //hasFmt设置为true/false
                callback(new Error("附件图片不能低于三张!"));
            } else {
                callback();
            }
        }
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                }
            },
            loading:false,
            isBtn:false,
            post:{},
            form: {
                store_id: '',// 门店ID
                name:'',// 姓名
                number:'',// 工号
                avatar:'',//头像
                phone:'',//手机号
                birthday:'',//生日
                position:'',//职位
                grade:'',//等级
                kill:'',//技能
                gender:'',//性别
                password:'',//密码
                source_id:'',//来源
                into_time:'',//入职时间
                formal_time:'',//转正时间
                nationality:'',//国籍：1中国、2泰国
                note:'',//备注
                id_type:'',//证件类型（居民身份证，护照，台港澳通行证，其它）
                id_card:'',//证件照
                annex:[],//附件
                expire_time:'',//合同到期时间（时间戳）
                nation:'',//民族
                address:'',//户籍地址
                education:'',//学历（初中，中专，高中，大专，本科，研究生，博士生，硕士生，其他）
                urgent_contacts:'',//紧急联系人
                urgent_phone:'',//紧急联系人电话
                card_number:'',//银行卡号
                marriage:'',//	婚姻状态
                reception_performance:'',//前台业绩目标
            },
            rules: {
                urgent_contacts: { required: true, message: '急联系人不能为空', trigger: 'blur' },
                urgent_phone: { required: true, message: '紧急联系人电话不能为空', trigger: 'blur' },
                id_type:{ required: true, message: '请选择证件类型', trigger: 'change' },
                id_card: { required: true, message: '证件号码不能为空', trigger: 'blur' },
                expire_time:{ required: true, message: '合同到期日期不能为空', trigger: 'blur' },
                nation:{ required: true, message: '民族不能为空', trigger: 'blur' },
                address:{ required: true, message: '户籍地址不能为空', trigger: 'blur' },
                education:{ required: true, message: '请选择学历', trigger: 'change' },
                // card_number:{ required: true, message: '银行卡号不能为空', trigger: 'blur' },
                gender:{ required: true, message: '请选择性别', trigger: 'change' },
                name: { required: true, message: '姓名不能为空', trigger: 'blur' },
                annex:[
                    { required: true, message: '请上传附件图片(身份证、护照等)!', trigger: 'blur' }
                ],
                reception_performance: { required: true, message: '前台业绩目标不能为空', trigger: 'blur' },
                avatar:{ required: true, message: '请选择头像', trigger: 'change' },
                number:{ required: true, message: '工号不能为空', trigger: 'blur' },
                phone:{ required: true, message: '手机号不能为空', trigger: 'blur' },
                birthday:{ required: true, message: '生日不能为空', trigger: 'blur' },
                password:{ required: false, message: '密码不能为空', trigger: 'blur' },
                position:{ required: true, message: '请选择职位', trigger: 'change' },
                nationality:{ required: true, message: '请选择国籍', trigger: 'change' },
                // grade:{ required: true, message: '等级不能为空', trigger: 'blur' },
                // kill:{ required: true, message: '技能不能为空', trigger: 'blur' },
                store_id:{ required: true, message: '请选择入门店', trigger: 'change' },
                into_time:{ required: true, message: '请选择入职日期', trigger: 'change' },
                formal_time:{ required: true, message: '请选择转正日期', trigger: 'change' },
            },
            roleList: [],
            postList:[],
            shopList:[],
        }
    },
    async created() {
        this.$route.meta.title = this.$route.query.title;
        this.rules.password.required = this.$route.query.title=='新增员工';
        this.homegetStore();
        if (this.$route.query.id) {
            this.form.id = this.$route.query.id;
           this.getinfo();
        }
        this.homegetPosition();
    },
    mounted() {
    },
    methods: {
        moneyIN(e){
            let value = e.target.value+"";
            value = value.replace(/[^\d.]/g,"");//清除"数字"和"."以外的字符
            value = value.replace(/^\./g,"");//验证第一个字符是数字而不是字符
            value = value.replace(/\.{2,}/g,".");//只保留第一个.清除多余的
            value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
            this.form.reception_performance = value||'';
        },
        async getinfo(){
            this.loading = true;
            let {code,data} = await this.$Http.getStaff({id:this.form.id});
            this.loading = false;
            if(code){
                this.post = data.position;
                this.form = {
                    id:data.id,
                    name:data.name,
                    number:data.number,
                    avatar:data.avatar,
                    phone:data.phone,
                    birthday:data.birthday,
                    position:data.position.id,
                    grade:data.grade,
                    kill:data.kill,
                    source_id:data.source_id?Number(data.source_id):'',
                    into_time:data.into_time*1000,
                    formal_time:data.formal_time*1000,
                    store_id:data.store_id,
                    note:data.note,
                    store_id:data.store_id?Number(data.store_id):'',
                    nationality:data.nationality,
                    password:'',
                    reception_performance:data.reception_performance,//前台业绩目标

                    gender:data.gender==0?'':data.gender,
                    id_type:data.id_type,//证件类型（居民身份证，护照，台港澳通行证，其它）
                    id_card:data.id_card,//证件照
                    annex:data.annex?data.annex.split(','):[],//附件
                    expire_time:data.expire_time*1000,//合同到期时间（时间戳）
                    nation:data.nation,//民族
                    address:data.address,//户籍地址
                    education:data.education,//学历（初中，中专，高中，大专，本科，研究生，博士生，硕士生，其他）
                    urgent_contacts:data.urgent_contacts,//紧急联系人
                    urgent_phone:data.urgent_phone,//紧急联系人电话
                    card_number:data.card_number,//银行卡号
                    marriage:data.marriage,//	婚姻状态
                }
                // this.chaposition(data.position.id)
            }
        },
        chaposition(e){
            // console.log( this.postList.find(v=>v.id==e),'chaposition');
            let val = this.postList.find(v=>v.id==e);
            this.post = val;
            if(val){
                this.form.grade = val.grade;
            }else this.form.grade = '';
           
        },
        async homegetPosition(){
            let {code,data} = await this.$Http.homegetPosition();
            if(code){
                this.postList = data;
            }
        },
        async homegetStore(){
            let {code,data} = await this.$Http.homegetStore();
            if(code){
                this.shopList = data;
            }
        },
        verify(hasFmt,sta){
            console.log(sta,"sta",this.form.avatar);
            sta?this.$refs[hasFmt].clearValidate():this.$refs[hasFmt].validate((valid)=>{
                console.log(valid,'validvalidvalid');
            });
            // setTimeout(()=>{
            //     if(sta){
            //         ;
            //     }else 
            // })
        },
        save() {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    this.isBtn = true;
                    let obj = Object.assign({},this.form);
                    obj.into_time/=1000;
                    obj.formal_time/=1000;
                    obj.expire_time/=1000;
                    obj.annex = obj.annex.join(',');
                    if(!this.form.id&&!obj.password)obj.password = '123456';
                    let {code} = await this.$Http.staffSave(obj);
                    this.isBtn = false;
                    if (code) {
                        if (this.form.id) {
                            this.$message.success('编辑成功')
                        } else {
                            this.$message.success('添加成功')
                        }
                        this.$router.go(-1)
                    }
                } else {
                    return false;
                }
            });
        },
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    section {
        .contentBox {
            background: #fff;
            border-radius: 8px;
            padding: 0 24px 20px;
            min-width: 900px;
            min-height: calc(100vh - 165px );
            .title {
                line-height: 70px;
                font-size: 18px;
                font-weight: 600;
                color: #383b40;
                .el-icon-close {
                    cursor: pointer;
                }
            }
        }
        .content {
            max-width:1200px;
            height: 100%;
            .form {
                display: flex;
                flex-wrap: wrap;
                // grid-row-gap: 20px;
                // grid-column-gap: 70px;
                justify-content: space-between;
            }
        }
    }
}
::v-deep{
    .el-input{
        width: 250px;
    }
}
</style>